#!/bin/bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"

cmd=$( basename "$0" )

caffeName='ts_caffe'

function usage() {
	echo "Usage: ${cmd} [ -t | -o | -h | -p]"
    echo -e "\t-t: 增加对src/gtest代码的索引"
    echo -e "\t-o: 旧版caffe,tsm->ts以前的版本"
    echo -e "\t-p: 增加对python接口的索引"
    echo -e "\t-h: 显示帮助信息"
	exit 1
}

# 默认不索引test目录下的文件
findTestOpt="-name test -type d -prune -o"
pythonApi=false
while getopts "toph" args
do
    case "${args}" in 
        t)
            # 包括gtest目录
            findTestOpt=""
            ;;
        o)
            caffeName='tsm_caffe'
            ;;
        p)
            pythonApi=true    
            ;;
        *)
            usage;;
    esac
done

protoFile="src/${caffeName}/proto/${caffeName}.proto"
# 包括.c,.cc文件所在目录
cppDirs=(src/${caffeName} src/${caffeName}/proto tools python)
cuDirs=(src/${caffeName} src/${caffeName}/proto tools python)
# 包含.h文件所在目录
hppDirs=(include/${caffeName})

# 包括gtest目录
if [ -z "${findTestOpt}" ]
then
    cppDirs[${#cppDirs[@]}]="src/gtest"
    hppDirs[${#hppDirs[@]}]="src/gtest"
fi

root="."
for d in "${srdDirs[@]}"
do
    dir_exists_or_die "${root}/$d"
done

csFile="${root}/cscope.files"
echo "Build ${csFile} ..."
# 充值文件列表
echo > "${csFile}"
for d in "${hppDirs[@]}"
do
    find "${root}/$d" ${findTestOpt} -name "*.hpp" -type f -print >> "${csFile}"
    find "${root}/$d" ${findTestOpt} -name "*.h" -type f -print >> "${csFile}"
done
for d in "${cppDirs[@]}"
do
    find "${root}/$d" ${findTestOpt} -name "*.cpp" -type f -print >> "${csFile}"
    find "${root}/$d" ${findTestOpt} -name "*.c" -type f -print >> "${csFile}"
    find "${root}/$d" ${findTestOpt} -name "*.cc" -type f -print >> "${csFile}"
    find "${root}/$d" ${findTestOpt} -name "*.cu" -type f -print >> "${csFile}"
done

if ${pythonApi}
then
    find "${root}/python" -name "*.py" -type f -print >> "${csFile}"
fi

# 包括proto文件
echo "${root}/${protoFile}"  >> "${csFile}"

# 建立cscope数据库
echo "Build cscope DB..."
cscope -Rbqk
echo "Done."
